/**
 * 安卓白金英雄坛制作组 <br>
 * 文件名：MainCharTile.java <br>
 * 创建时间：2013-5-20 下午4:54:37 <br>
 * 所属项目：GmudEX <br>
 * @author 12548 <br>
 */
package lostland.gmud.exv2;

import lostland.gmud.exv2.blgframework.impl.BLGGraphics;
import lostland.gmud.exv2.blgframework.impl.BLGPixmap;

/**
 * 类名：MainCharTile <br>
 * 说明：主角移动方略
 *
 * @author 12548
 */
public class MainCharTile extends Tileset {

	final static int MAINCHAR_WIDTH = 32;
	final static int MAINCHAR_HEIGHT = 48;

	final static int WALK_STEPS = 4;

	public int currentStep = 0;
	public int X, Y;

	public Direction currentDirection = Direction.DOWN;

	private MainCharTile() {
		super((BLGPixmap) Assets.getMainchar(), MAINCHAR_WIDTH, MAINCHAR_HEIGHT);
	}

	private static MainCharTile _instance;

	public static MainCharTile getInstance() {
		if (_instance == null) {
			_instance = new MainCharTile();
		}
		return _instance;
	}


	/**
	 * @param direction 主角朝向
	 * @param step      主角步伐，对4取余后0、2为静止，1为出右脚，3为出左脚。
	 * @param x         主角相对屏幕的X坐标（0-4）
	 * @param y         主角相对屏幕的Y坐标（0-2）
	 */
	public void drawInMap(Direction direction, int step, int x, int y) {
		BLGPixmap pixmap = (BLGPixmap) Assets.getMainchar();
		if (Gmud.mc.sex > 0) {
			pixmap = (BLGPixmap) Assets.getGirl();
		}
		((BLGGraphics) Game.getInstance().getGraphics()).drawPixmapInMap(pixmap, x * MAINCHAR_WIDTH, (y + 1) * MAINCHAR_WIDTH - MAINCHAR_HEIGHT
				, (step % 4) * tileWidth, direction.maincharTileY() * tileHeight, tileWidth + 1, tileHeight + 1);
		currentStep = step % 4;
		currentDirection = direction;
		X = x;
		Y = y;
	}

	/**
	 * 画出原地踏步的下一帧。
	 *
	 * @param direction 方向
	 * @param x         主角相对屏幕的X坐标（0-4）
	 * @param y         主角相对屏幕的Y坐标（0-2）
	 */
	public void drawOriWalking(Direction direction, int x, int y) {
		BLGPixmap pixmap = (BLGPixmap) Assets.getMainchar();
		if (Gmud.mc.sex > 0) {
			pixmap = (BLGPixmap) Assets.getGirl();
		}
		Game.getInstance();
		((BLGGraphics) Game.getInstance().getGraphics()).drawPixmapInMap(pixmap, x * MAINCHAR_WIDTH, (y + 1) * MAINCHAR_WIDTH - MAINCHAR_HEIGHT
				, currentStep * tileWidth, direction.maincharTileY() * tileHeight, tileWidth + 1, tileHeight + 1);
		currentDirection = direction;
	}

	/**
	 * 画出地图内左右移动的下一帧。
	 *
	 * @param direction 方向，只能为LEFT或RIGHT。
	 * @param step      走出的第N-1步（走到下一个格子共需WALK_STEPS步）
	 * @param x         出发点x坐标，1、2或3。
	 */
	public void drawInnerWalking(Direction direction, int step, int x, int y) {
		BLGPixmap pixmap = (BLGPixmap) Assets.getMainchar();
		if (Gmud.mc.sex > 0) {
			pixmap = (BLGPixmap) Assets.getGirl();
		}

		int dx = (direction.dx()) * MAINCHAR_WIDTH * (step + 1) / WALK_STEPS;
		int dy = direction.dy() * MAINCHAR_WIDTH * (step + 1) / WALK_STEPS;

		((BLGGraphics) Game.getInstance().getGraphics()).drawPixmapInMap(pixmap, x * MAINCHAR_WIDTH + dx, (y + 1) * MAINCHAR_WIDTH - MAINCHAR_HEIGHT + dy
				, currentStep * tileWidth, direction.maincharTileY() * tileHeight, tileWidth + 1, tileHeight + 1);
		currentDirection = direction;
	}

	public static int absX() {
		return MapScreen.X + getInstance().X;
	}

	public static int absY() {
		return MapScreen.Y + getInstance().Y;
	}

	public static int frontX() {
		return getInstance().X + getInstance().currentDirection.dx();
	}

	public static int frontY() {
		return getInstance().Y + getInstance().currentDirection.dy();
	}

	public static int frontAbsX() {
		return absX() + getInstance().currentDirection.dx();
	}

	public static int frontAbsY() {
		return absY() + getInstance().currentDirection.dy();
	}


}
